/*
https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/zhi-shao-you-kge-zhong-fu-zi-fu-de-zui-c-o6ww/
 */
public class Solution395 {
    public int longestSubstring(String s, int k) {
        int[] f=new int[26];
        for (char c : s.toCharArray()) {
            f[c-'a']++;
        }
        int flag=-1;
        for (int i=0;i<26;i++){
            if (f[i]>0 && f[i]<k){
                flag=i;
                break;
            }
        }
        if (flag==-1){
            return s.length();
        }
        int ans=0;
        char c=(char) (flag+'a');
        String[] strings=s.split(String.valueOf(c));
        for (String string : strings) {
            ans=Math.max(ans,longestSubstring(string,k));
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution395().longestSubstring("aaabb",3));
    }

}
